package com.wct.permission.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @author WEI (Email:changtan20000@163.com)
 * @description
 * @date 2020/2/11 14:12
 */
@Service
@Slf4j
public class ImageCodeService {

    // 能使用的字符集
    private final static String[] word = {
            "a", "b", "c", "d", "e", "f", "g",
            "h", "j", "k", "m", "n",
            "p", "q", "r", "s", "t",
            "u", "v", "w", "x", "y","z",
            "A", "B", "C", "D", "E", "F", "G",
            "H", "J", "K", "M", "N",
            "P", "Q", "R", "S", "T",
            "U", "V", "W", "Z", "Y", "Z"
    };
    // 数字
    private final static String[] num = {
            "2", "3", "4", "5", "6", "7", "8", "9"
    };

    public BufferedImage createImageCode(HttpSession session) {
        BufferedImage bfi = new BufferedImage(80, 25, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bfi.getGraphics();
        graphics.fillRect(0, 0, 80, 25);

        //验证码字符范围
//        char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
        Random random = new Random();
        int index;
        StringBuilder stringBuilder = new StringBuilder(); //保存字符串
        for (int i = 0; i < 4; i++) {
            String s;
            if (random.nextBoolean()){
                index = random.nextInt(word.length);
                s = word[index];
            } else {
                index = random.nextInt(num.length);
                s = num[index];
            }

            graphics.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
            Font font = new Font("宋体", 30, 20);
            graphics.setFont(font);
            graphics.drawString(s, (i * 20) + 2, 23);
            stringBuilder.append(s);
        }

        // 添加噪点
        int area = (int) (0.02 * 80 * 25);
        for (int i = 0; i < area; ++i) {
            int x = (int) (Math.random() * 80);
            int y = (int) (Math.random() * 25);
            bfi.setRGB(x, y, (int) (Math.random() * 255));
        }

        //设置验证码中的干扰线
        for (int i = 0; i < 6; i++) {
            //随机获取干扰线的起点和终点
            int xStart = (int) (Math.random() * 80);
            int yStart = (int) (Math.random() * 25);
            int xEnd = (int) (Math.random() * 80);
            int yEnd = (int) (Math.random() * 25);
            graphics.setColor(interLine(1, 255));
            graphics.drawLine(xStart, yStart, xEnd, yEnd);
        }
        // 把验证码添加到session中
        log.info("imageCode: {}", stringBuilder.toString());
        session.setAttribute("verificationCode", stringBuilder.toString());
        return bfi;
    }


    private static Color interLine(int Low, int High) {
        if (Low > 255) {
            Low = 255;
        }
        if (High > 255) {
            High = 255;
        }
        if (Low < 0) {
            Low = 0;
        }
        if (High < 0) {
            High = 0;
        }
        int interval = High - Low;
        int r = Low + (int) (Math.random() * interval);
        int g = Low + (int) (Math.random() * interval);
        int b = Low + (int) (Math.random() * interval);
        return new Color(r, g, b);
    }
}
